<?php
/**
 * Minhlog 后台添加功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Backend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: AddController.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Backend_AddController extends Minh_Controller_Action
{
    protected $_articleModel;
    protected $_categoryModel;
    protected $_tagModel;
    protected $_commentModel;
    protected $_pageModel;
    protected $_linkModel;
    protected $_userModel;

    public function init()
    {
        if (!isset($_SESSION['Minhlog']['storage']->user_id)) {
            $this->_redirect('/backend/user/login/');
        }

        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        if(null === $this->_categoryModel){
            if(class_exists('Domain_Model_Category')){
                $this->_categoryModel = new Domain_Model_Category();
            } else {
                throw new Exception('文章分类模型类不存在!');
            }
        }

        if(null === $this->_tagModel){
            if(class_exists('Domain_Model_Tag')){
                $this->_tagModel = new Domain_Model_Tag();
            } else {
                throw new Exception('标签模型类不存在!');
            }
        }

        if(null === $this->_commentModel){
            if(class_exists('Domain_Model_Comment')){
                $this->_commentModel = new Domain_Model_Comment();
            } else {
                throw new Exception('文章评论模型类不存在!');
            }
        }

        if(null === $this->_pageModel){
            if(class_exists('Domain_Model_Page')){
                $this->_pageModel = new Domain_Model_Page();
            } else {
                throw new Exception('页面模型类不存在!');
            }
        }

        if(null === $this->_linkModel){
            if(class_exists('Domain_Model_Link')){
                $this->_linkModel = new Domain_Model_Link();
            } else {
                throw new Exception('友情链接模型类不存在!');
            }
        }

        if(null === $this->_userModel){
            if(class_exists('Domain_Model_User')){
                $this->_userModel = new Domain_Model_User();
            } else {
                throw new Exception('用户模型类不存在!');
            }
        }

        parent::init();
    }

    public function articleAction ()
    {
        if($this->getRequest()->isPost()) {
            $articleData = array(
                    'author_id' => $this->_getParam('author_id',1),
                    'category_id' => $this->_getParam('category_id', 0),
                    'article_title' => $this->_getParam('article_title', ''),
                    'article_content' => $this->_getParam('article_content', ''),
                    'article_summary' => $this->_getParam('article_summary', ''),
                    'publish_time' => time()
            );
            $articleId = $this->_articleModel->saveArticle($articleData);

            //查询Tag列表
            $tagList = $this->_tagModel->getAllTag();
            $tagArray = array();
            foreach ($tagList as $t){
                if(strtolower($t->tagName) != ''){
                    $tagArray[$t->tagId] = strtolower($t->tagName);
                }
            }

            $tags = explode(',',$this->_getParam('article_tags'));
            foreach($tags as $tag){
                //判断Tag是否存在，如果存在则跳过
                $hasTagId = array_search(strtolower($tag),$tagArray);

                if($hasTagId !== false){ //标签存在
                    $articleTagData = array(
                            'article_id' => $articleId,
                            'tag_id' => $hasTagId
                    );

                    $hasTagRelation = $this->_tagModel->checkTagRelation($articleId, $hasTagId);

                    if(null === $hasTagRelation){
                        $this->_tagModel->saveTagRelation($articleTagData);
                    }
                    continue;
                } else { //标签不存在
                    $tagData = array(
                            'tag_name' =>$tag
                    );
                    $tagId = $this->_tagModel->saveTag($tagData);
                    $articleTagData = array(
                            'article_id' => $articleId,
                            'tag_id' => $tagId
                    );
                    $this->_tagModel->saveTagRelation($articleTagData);
                }
            }
            $this->_redirect('/backend/list/article/');
        }

        $categoryTree = $this->_categoryModel->getCategoryTree();
        if(null !== $categoryTree) $this->view->categoryTree = $categoryTree;
        $this->view->menuSelected = 1;
        echo $this->view->render('articleedit.phtml');
    }

    public function categoryAction()
    {
        if($this->getRequest()->isPost()){
            $categoryData = array(
                    'parent_id' => $this->_getParam('parent_id', 0),
                    'category_name' => $this->_getParam('category_name', ''),
                    'category_desc' => $this->_getParam('category_desc', ''),
            );
            $categoryId = $this->_categoryModel->saveCategory($categoryData);
            if($categoryId > 0){
                $this->_redirect('/backend/list/category/');
            }
        }
    }

    public function linkAction()
    {
        if($this->getRequest()->isPost()){
            $filter = new Zend_Filter_Int();
            $linkData = array(
                    'type_id' => $filter->filter($this->_getParam('add_type_id')),
                    'site_name' => $this->_getParam('site_name'),
                    'site_desc' => $this->_getParam('site_desc'),
                    'site_url' => $this->_getParam('site_url')
            );
            $linkId = $this->_linkModel->saveLink($linkData);
            if($linkId > 0){
                $this->_redirect('/backend/list/link/');
            }
        }
    }

    public function linktypeAction()
    {
        if($this->getRequest()->isPost()){
            $linkTypeData = array(
                    'type_name' => $this->_getParam('type_name'),
                    'type_desc' => $this->_getParam('type_desc')
            );
            $linkTypeId = $this->_linkModel->saveLinkType($linkTypeData);
            if($linkTypeId > 0){
                $this->_redirect('/backend/list/linktype/');
            }
        }
    }

    public function pageAction()
    {
        if($this->getRequest()->isPost()){
            $pageData = array(
                    'page_title' => $this->_getParam('page_title'),
                    'page_content' => $this->_getParam('page_content'),
                    'publish_time' => time()
            );
            $pageId = $this->_pageModel->savePage($pageData);
            if($pageId > 0){
                $this->_redirect('/backend/list/page/');
            }
        }
        $this->view->menuSelected = 301;
        echo $this->view->render('pageadd.phtml');
    }
}